package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.hontech.bread.R

class PointsParts(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

    companion object {
        const val POINT_SIZE = 25
        const val MARGIN = 10
    }

    var index = 0
        set(value) {
            field = value
            select()
        }

    var count = 1
        set(value) {
            field = value
            update()
        }

    init {
        orientation = HORIZONTAL
    }

    private fun update() {

        val param = LayoutParams(POINT_SIZE, POINT_SIZE)
        param.leftMargin = MARGIN
        removeAllViews()
        for (i in 0 until count) {
            val view = View(context)
            view.setBackgroundResource(R.drawable.point_white)
            addView(view, param)
        }
    }

    private fun select() {

        for (i in 0 until count) {
            val view = getChildAt(i)

            if (i == index) {
                view.setBackgroundResource(R.drawable.point_red2)
            } else {
                view.setBackgroundResource(R.drawable.point_white)
            }
        }
    }

}

